package com.easy.web.controller;

import com.easy.web.model.Article;
import com.easy.web.model.ArticleType;
import com.easy.web.service.ArticleService;
import com.easy.web.service.ArticleTypeService;
import com.easy.web.utils.ViewUtils;
import com.easy.web.vo.ArticleVo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/1/4.
 */
@Controller
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleTypeService articleTypeService;
    /**
     * 获取最新的文章
     */
    @RequestMapping(value = "getArticleList",method = {RequestMethod.GET})
    public ModelAndView getArticleList(@RequestParam(value = "tacticsNum",required = false) String limitNum,
                                       @RequestParam(value = "tacticsId",required = false) String type){
        List<Article> articleList=articleService.getArticleList(limitNum,type);
        List<ArticleVo> articleVoList=new ArrayList<>();
        for(Article article:articleList){
            ArticleVo articleVo=new ArticleVo(article);
            ArticleType articleType=articleTypeService.queryArticleTypeById(article.getArticleType());
            articleVo.setTypeName(articleType.getComment());
            articleVoList.add(articleVo);
        }
        return new ModelAndView("", ViewUtils.mapSuccess(articleVoList));
    }

    /**
     * 获取最新的文章类型
     */
    @RequestMapping(value = "getArticleTypeList",method = {RequestMethod.GET})
    public ModelAndView getArticleTypeList(){
        List<ArticleType> articleTypeList=articleTypeService.getArticleTypeList();
        return new ModelAndView("", ViewUtils.mapSuccess(articleTypeList));
    }
    /**
     * 新增一篇文章
     */
    @RequestMapping(value = "addArticle",method = {RequestMethod.POST})
    public ModelAndView addArticle(@RequestParam(value = "baseValue",required = true) String articleValue){
        JSONObject arJson=JSONObject.fromObject(articleValue);
        Article article =new Article();
        if(!arJson.isEmpty()){
            article.setId(UUID.randomUUID().toString());
            article.setArticleTitle(arJson.optString("setTacticsTitle"));
            article.setArticleAuthor(arJson.optString("setTacticsAuthor"));
            article.setAuthorPublisher(arJson.optString("publisher"));
            article.setArticleContent(arJson.optString("setTacticsContent"));
            article.setCreateTime(new Date());
            article.setArticleCover(arJson.optString("setTacticsCover"));
            article.setArticleType(arJson.optString("setTacticsType"));
            article.setArticleTime(arJson.optString("setTacticsTime"));
            articleService.addArticle(article);
        }
        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 根据id查询文章
     */
    @RequestMapping(value = "queryArticle",method = {RequestMethod.GET})
    public ModelAndView queryArticle(@RequestParam(value = "editTacticsUid",required = true) String id){
        Article article =articleService.queryArticleById(id);
        ArticleVo articleVo=new ArticleVo(article);
        ArticleType articleType=articleTypeService.queryArticleTypeById(article.getArticleType());
        articleVo.setTypeName(articleType.getComment());
        return new ModelAndView("", ViewUtils.mapSuccess(articleVo));
    }

    /**
     * 编辑文章
     */
    @RequestMapping(value = "updateArticle",method = {RequestMethod.POST})
    public ModelAndView updateArticle(@RequestParam(value = "baseValue",required = true) String articleValue){
        JSONObject arJson=JSONObject.fromObject(articleValue);
        if(!arJson.isEmpty()){
            Article article=articleService.queryArticleById(arJson.optString("editTacticsUid"));
            if(article!=null){
                article.setArticleTitle(arJson.optString("editTacticsTitle"));
                article.setArticleAuthor(arJson.optString("editTacticsAuthor"));
                article.setArticleCover(arJson.optString("editTacticsCover"));
                article.setArticleContent(arJson.optString("editTacticsContent"));
                article.setAuthorPublisher(arJson.optString("publisher"));
                article.setArticleType(arJson.optString("editTacticsType"));
                article.setArticleTime(arJson.optString("editTacticsTime"));
                articleService.updateArticle(article);
            }else {
                return new ModelAndView("", ViewUtils.mapFail("此文章不存在，请重新选择"));
            }

        }
        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 删除文章
     */
    @RequestMapping(value = "delArticle",method = {RequestMethod.POST})
    public ModelAndView delArticle(@RequestParam(value = "delTacticsUid",required = true) String id){
        articleService.delArticle(id);
        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 查询文章
     */
    @RequestMapping(value = "getArticlesByExample",method = {RequestMethod.POST})
    public ModelAndView getArticlesByExample(@RequestParam(value = "baseValue",required = true) String baseValue){
        JSONObject arJson=JSONObject.fromObject(baseValue);
        List<ArticleVo> articleVoList=new ArrayList<>();
        if(!arJson.isEmpty()){
            List<Article> articleList=articleService.getArticlesByExample(arJson);
            for(Article article:articleList){
                ArticleType articleType=articleTypeService.queryArticleTypeById(article.getArticleType());
                ArticleVo articleVo=new ArticleVo(article);
                articleVo.setTypeName(articleType.getComment());
                articleVoList.add(articleVo);
            }
         }
        return new ModelAndView("", ViewUtils.mapSuccess(articleVoList));
    }
}
